﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Reflection;

namespace BSC_OTC_THONGBAO
{
    class SendEmail
    {
        #region khai bao bien
        private Code.CL_Ultility _cl = new Code.CL_Ultility();
        string EmailTo = ConfigurationManager.AppSettings["EmailTo"].ToString();
        string EmailAddress = ConfigurationManager.AppSettings["EmailAddress"].ToString();
        string EmailPassword = ConfigurationManager.AppSettings["EmailPassword"].ToString();
        string EmailDisplay = ConfigurationManager.AppSettings["EmailDisplay"].ToString();
        string EmailHost = ConfigurationManager.AppSettings["EmailHost"].ToString();
        string EmailPort = ConfigurationManager.AppSettings["EmailPort"].ToString();
        string ThuMucContent = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"Content.html");
            //= ConfigurationManager.AppSettings["ThuMucContent"].ToString();
        #endregion

        public SendEmail(string _soHD, string _tenHD, string _chitietCV, string _tgCanhBao, string _tgthuchien)
        {
            // khoi tao tham so
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(EmailAddress);
            msg.IsBodyHtml = true;
            msg.Subject = this.EmailDisplay;
            msg.Body = this.ContentEmail(_soHD, _tenHD, _chitietCV, _tgCanhBao, _tgthuchien);
            msg.To.Clear();

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential(this.EmailAddress, this.EmailPassword);
            smtpClient.Port = Convert.ToInt32( this.EmailPort);
            smtpClient.Host = this.EmailHost;

            // lay danh sach nhung ng se gui email thong bao
            if (this.EmailTo.Contains(";"))
            {
                string[] emailaddress = this.EmailTo.Split(';');
                foreach (string item in emailaddress)
                {
                    if (this.IsValidEmail(item))
                    {
                        try
                        {
                            msg.To.Clear();
                            msg.To.Add(item);
                            smtpClient.Send(msg);
                        }
                        catch (Exception ex)
                        {
                            _cl.UPDATE_LOG_FILE("ERR", "", "SendEmail()", "SendEmail()", ex.Message);
                        }
                    }
                }
            }
            else
            {
                if (this.IsValidEmail(this.EmailTo))
                {
                    msg.To.Clear();
                    msg.To.Add(this.EmailTo);
                    smtpClient.Send(msg);
                }
            }

            _cl.UPDATE_LOG_FILE("file_data", "", "SendEmail()", "SendEmail()", msg.From + "/" +msg.To +"/"+msg.Subject+"/"+msg.Subject);
        }
        
        private string ContentEmail(string _soHD, string _tenHD, string _chitietCV, string _tgCanhBao, string _tgthuchien)
        {
            string result = File.ReadAllText(ThuMucContent);
            result = result.Replace("TENCONGVIEC", _chitietCV);
            result = result.Replace("NGAYCANHBAO", _tgCanhBao);
            result = result.Replace("NGAYTHUCHIEN", _tgthuchien);
            result = result.Replace("SOHD", _soHD);
            result = result.Replace("TENHD", _tenHD);
            return result;
        }

        bool IsValidEmail(string email)
        {
            try
            {
                var addr = new System.Net.Mail.MailAddress(email);
                return addr.Address == email;
            }
            catch
            {
                return false;
            }
        }

    }
}